package com.nl.web.filter;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * header拦截器把header数据放到MDC.
 *
 * @author breggor
 */
@Slf4j
@Component
public class HeaderFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws ServletException, IOException {
//        MDC.put(CoreConsts.GW_USER_ID, StringUtils.hasText(request.getHeader(CoreConsts.GW_USER_ID))
//            ? (StringUtils.hasLength(request.getParameter(CoreConsts.GW_USER_ID)) ? ConstPool.DASH : request.getParameter(CoreConsts.GW_USER_ID))
//            : request.getHeader(CoreConsts.GW_USER_ID));

        try {
            chain.doFilter(request, response);
        } finally {
            MDC.clear();
        }
    }

}
